package com.example.myapplication.apter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.LegalexpertiseBean;
import com.example.myapplication.lawyer.AddconsultationlistActivity;
import com.example.myapplication.lawyer.LawyerActivity;
import com.example.myapplication.lawyer.LawyerListActivity;
import com.example.myapplication.lawyer.LawyerconsultationActivity;
import com.example.myapplication.lawyer.LawyerlistpageActivity;
import com.example.myapplication.lawyer.LegalexpertiseActivity;

import com.google.gson.Gson;

import java.util.List;
import java.util.Map;

public class LegalexpertiseApter extends RecyclerView.Adapter<LegalexpertiseApter.MyServer> {
    private Context context;
    private List<LegalexpertiseBean.RowsBean> list;
    private View view;
    private Gson gson;
    private LegalexpertiseBean legalexpertiseBean;
    private Integer  LawyerID ;
    private String   LawyerName ;
    private String   TAG = "TAG" ;

    public LegalexpertiseApter(Context context, List<LegalexpertiseBean.RowsBean> list, LegalexpertiseBean legalexpertiseBean) {
        this.context = context;
        this.list = list;
        this.legalexpertiseBean = legalexpertiseBean;
    }

    public LegalexpertiseApter(Context context, List<LegalexpertiseBean.RowsBean> list) {
        this.context = context;
        this.list = list;
        legalexpertiseBean = new LegalexpertiseBean();
        legalexpertiseBean.setRows(list);
        //编译类型  运行类型
        // Map<Object, Object> objectObjectMap = new Map<>();

    }

    public Integer getLawyerID(){
        return  LawyerID;
    }

    public String getLawyerName(){
        return  LawyerName;
    }


    @NonNull
    @Override
    public LegalexpertiseApter.MyServer onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //填充试图
        view = LayoutInflater.from(context).inflate(R.layout.item_layers, null);
        if (context instanceof LawyerActivity) {
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(270, 270);
            view.setLayoutParams(layoutParams);
        } else if (context instanceof LegalexpertiseActivity) {
            Log.v("LegalexpertiseActivity", "=========");
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(270, 200);
            view.setLayoutParams(layoutParams);
        }
        return new LegalexpertiseApter.MyServer(view);
    }

    @Override
    public void onBindViewHolder(@NonNull LegalexpertiseApter.MyServer holder, int position) {
        Glide.with(context).load(TokenBean.IP + list.get(position).getImgUrl()).into(holder.sever_tv_img);
        holder.sever_iv_text.setText(list.get(position).getName());
        gson = new Gson();
        String JSONStringDate = gson.toJson(legalexpertiseBean);
       // String JSONStringDateList = gson.toJson(list);
        //进入法律专长列表页
        if (context instanceof LawyerActivity) {
            holder.li_layers.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, LegalexpertiseActivity.class);
                    intent.putExtra("JSONStringDate", JSONStringDate);
                    context.startActivity(intent);
                }
            });
        } else if (context instanceof LegalexpertiseActivity){
            //进入律师列表页
            holder.li_layers.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, LawyerListActivity.class);
                    intent.putExtra("LawyerListActivity", JSONStringDate);
                    context.startActivity(intent);
                }
            });
        }else  if(context instanceof LawyerListActivity){
            holder.li_layers.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, LawyerlistpageActivity.class);
                    intent.putExtra("LawyerlistpageActivityID",String.valueOf(list.get(position).getId()));
                    LawyerID =  list.get(position).getId();
                }
            });
        }else  if(context instanceof AddconsultationlistActivity){
            holder.li_layers.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    LawyerID = list.get(position).getId() ;

                }
            });
        }else if(context instanceof LawyerconsultationActivity){
            holder.li_layers.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    LawyerID = list.get(position).getId() ;
                    LawyerName = list.get(position).getName() ;
                }
            });
        }

    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyServer extends RecyclerView.ViewHolder {

        private final TextView sever_iv_text;
        private final ImageView sever_tv_img;
        private final LinearLayout li_layers;

        public MyServer(@NonNull View itemView) {
            super(itemView);
            sever_iv_text = itemView.findViewById(R.id.sever_iv_text);
            sever_tv_img = itemView.findViewById(R.id.sever_tv_img);
            li_layers = itemView.findViewById(R.id.li_layers);
        }
    }
}
